//用户数据示例
let users = [{
		"uuid": "08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a",
		"name": "Mattie",
		"password": "123",
		"avatar": '/static/images/Avatar-1.png'
	},
	{
		"uuid": "3bb179af-bcc5-4fe0-9dac-c05688484649",
		"name": "Wallace",
		"password": "123",
		"avatar": '/static/images/Avatar-2.png'
	},
	{
		"uuid": "fdee46b0-4b01-4590-bdba-6586d7617f95",
		"name": "Tracy",
		"password": "123",
		"avatar": '/static/images/Avatar-3.png'
	},
	{
		"uuid": "33c3693b-dbb0-4bc9-99c6-fa77b9eb763f",
		"name": "Juanita",
		"password": "123",
		"avatar": '/static/images/Avatar-4.png'
	}
];

//群数据示例
let groups = [{
		"uuid": "group-a42b-47b2-bb1e-15e0f5f9a19a",
		"name": "小程序交流群",
		"avatar": '/static/images/wx.png',
		"userList": ['08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a', '3bb179af-bcc5-4fe0-9dac-c05688484649',
			'fdee46b0-4b01-4590-bdba-6586d7617f95', '33c3693b-dbb0-4bc9-99c6-fa77b9eb763f'
		]
	},
	{
		"uuid": "group-4b01-4590-bdba-6586d7617f95",
		"name": "UniApp交流群",
		"avatar": '/static/images/uniapp.png',
		"userList": ['08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a', 'fdee46b0-4b01-4590-bdba-6586d7617f95',
			'33c3693b-dbb0-4bc9-99c6-fa77b9eb763f'
		]
	},
	{
		"uuid": "group-dbb0-4bc9-99c6-fa77b9eb763f",
		"name": "GoEasy交流群",
		"avatar": '/static/images/goeasy.jpeg',
		"userList": ['08c0a6ec-a42b-47b2-bb1e-15e0f5f9a19a', '3bb179af-bcc5-4fe0-9dac-c05688484649']
	}
];


function RestApi() {

}

RestApi.prototype.findFriends = function(user) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.baseURL + '/user/all',
			method: 'POST',
			header: {
				'Content-Type': 'application/json'
			},
			success: (res) => {
				// console.log("findFriends 结果为 " + JSON.stringify(res.data.data));
				var friendList = new Array();
				for (let index in res.data.data) {
					if(res.data.data[index].userId !== user.uuid){
						friendList.push({
							uuid: res.data.data[index].userId,
							name: res.data.data[index].username,
							avatar: res.data.data[index].avatarUrl
						})
					}
				}
				resolve(friendList);
			},
			fail: (res) => {
				console.log("查找用户失败！");
				resolve(null);
			}
		})
	})

	// var friendList = users.filter(v => v.uuid != user.uuid);
	// return friendList;
}

RestApi.prototype.findGroups = function(user) {
	var groupList = groups.filter(v => v.userList.find(id => id == user.uuid));
	return groupList;
}

RestApi.prototype.findUser = function(username, password) {
	let that = this;
	return new Promise((resolve, reject) => {
		var loginUser = {
			username: username,
			password: password
		}
		uni.request({
			url: getApp().globalData.baseURL + '/user/login',
			method: 'POST',
			data: loginUser,
			header: {
				'Content-Type': 'application/json'
			},
			success: res => {
				// console.log('res为 ' + JSON.stringify(res.data));
				let user = {
					uuid: res.data.data.userId,
					avatar: res.data.data.avatarUrl,
					name: res.data.data.username,
					password: res.data.data.password
				}
				resolve(user);
			},
			fail: res => {
				console.log('连接失败了呜呜呜! (QAQ)');
				resolve('fail');
				return null;
			}
		});
	})
}

RestApi.prototype.findGroupById = function(groupId) {
	var group = groups.find(group => (group.uuid == groupId));
	return group;
};


RestApi.prototype.findUserById = function(userId) {
	return new Promise( (resolve, reject) =>{
		uni.request({
			url: getApp().globalData.baseURL + '/user/user_id/' + userId,
			success: (res) => {
				console.log("findUserById 的结果为 " + res.data.data);
				var user = {
					uuid: res.data.data.userId,
					name: res.data.data.username,
					avatar: res.data.data.avatarUrl
				}
				resolve(user);
			},
			fail: (res) => {
				console.log("查找用户失败！");
				resolve(null);
			}
		})
	})
}



RestApi.prototype.findGroupMembers = function(groupId) {
	let members = [];
	let group = groups.find(v => v.uuid == groupId);
	users.map(user => {
		if (group.userList.find(v => v == user.uuid)) {
			members.push(user)
		}
	});
	return members;
}

export default new RestApi();
